<?php
// Noraml Functions
// Format saved text
function format_string($str) {
    // This will replace new lines with breaks
    $message = str_replace("\n", "<br />", $str);
    // This will replace spaces of more than 1 with &nbsp; as only one space is noticed though html
    $message = str_replace("  ", "&nbsp;", $message);
    //turns < into &lt;
    $message = str_replace("<", "&lt;", $message);
    $message = str_replace("'", "&#39;", $message);
   return $message;
}
// Make the id for each stored text
function makeName($n) {
$codeset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$base = strlen($codeset);
$converted = "";
while ($n > 0) {
$converted = substr($codeset, ($n % $base), 1) . $converted;
$n = floor($n/$base);
}
return $converted;
}
// Functions start here
// Post function
function post($text, $ps="pass", $mobile=0) {
	if ($ps == "pass") {
		include("settings.php"); // Include settings
	    $text = nl2br(htmlentities($text, ENT_QUOTES,'UTF-8')); // Main format
	    $text = str_replace("'", '&#39;', $text); // "Our" format
		// Check if entry allready exists
	    $query = @mysql_query("SELECT * FROM `$tableName` WHERE BINARY `text`='$text'") or die(mysql_error());
	    if(mysql_num_rows($query) != 0) {
	        while($row = @mysql_fetch_array($query)) {
                $tid = $row['tid'];
                }
	        $exists = "true";
	        $url = $domain . '/' . $tid;
	    }
		// If doesn't exists, add to database
	    if (!isset($exists)) {
	        $ip = $_SERVER['REMOTE_ADDR'];
			date_default_timezone_set("Europe/London"); 
			$time = date("j-m-Y")." ".date("G:i:s");
	        $query = @mysql_query("INSERT INTO `" . $tableName . "` (`text`, `ip`, `timestamp`) VALUES ('$text', '$ip', '$time')") or die(mysql_error());
	        $num = mysql_query("SELECT `id` FROM `$tableName`");
	        $num = @mysql_num_rows($num) or die(mysql_error());
	        $tid = makeName($num);
                if ($tid == "3-includes" || $tid == "3-content" || $tid == "3-admin") {
                $dir = "true";
                $query = @mysql_query("INSERT INTO `" . $tableName . "` (`text`, `ip`, `timestamp`) VALUES ('$text', '$ip', '$time')") or die(mysql_error());
                $num = mysql_query("SELECT `id` FROM `$tableName`");
                $num = @mysql_num_rows($num) or die(mysql_error());
                $tid = makeName($num);
                }
	        $query = @mysql_query("UPDATE `$tableName` SET `tid`='$tid' WHERE `id`='$num'") or die(mysql_error());
	        $url = $domain . "/" . $tid;
	    }
	    if ($mobile == 0) {
	    	$link = $url;
	    	return $link;
    	} else {
    		$link = '<a href="#viewer" class="slideup" id="linkA">'.$url.'</a>';
    		return $link."<div id='tid' style='display: none;'>".$tid."</div>";
    	}
	} else {
		return "INVALID_PS"; // Something went wrong
	}
}
// Show stored text
function show($id, $ps="pass", $mobile=0) {
	if ($ps == "pass") {
		include("settings.php"); // Include Settings
		$query = mysql_query("SELECT `text` from `$tableName` WHERE BINARY `tid`='$id'"); // Find text
		while($row=mysql_fetch_array($query)) {
			return $row['text']; // Return the text
		}
	} else {
		return "INVALID_PS"; // Something went wrong
	}
}

?>